﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO.Ports;
using System.Windows.Forms;

namespace Pda_Total_Station
{
    public class GeoCom
    {
        bool listen = false;
        string recive_msg;
        AutoResetEvent wait;
        /// <summary>
        /// 
        /// </summary>
        public SerialPort port;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="COM_Name">COM口的名字</param>
        public GeoCom(string COM_Name)
        {
            if (port == null)
            {
                port = new SerialPort();
            }
            if (port.IsOpen == true)
            {
                port.Close();
            }
            this.port = OpenPort(COM_Name, 9600, 8, 300, 300);

        }

        #region Measure_Distance_Angle
        /// <summary>
        /// 测量Hz,Hv和Dist
        /// </summary>
        /// <returns> 返回格式:"水平角(°.′″),垂直角,距离,如果测距失败,距离返回-1，超时返回0"</returns>
        public string Measure_Distance_Angle()
        {

            measure_del del = new measure_del(measure_dist_angle_thr);
            del.Invoke();
            string val = recive_msg;
            return val;
        }
        delegate void measure_del();
        void measure_dist_angle_thr()
        {
            if (listen == true)
            {
                return;
            }
            send("%R1Q,17017:2\r\n");
            wait.WaitOne(20000, false);
            if (recive_msg != null && recive_msg.Length > 0)
            {
                recive_msg = recive_msg.Replace("'", string.Empty);

                string[] receive = recive_msg.Split(Convert.ToChar(","));
                if (receive[2] == "0:0")
                {
                    string hz = receive[3];
                    string hv = receive[4];
                    double dist = Math.Round(Convert.ToDouble(receive[5]), 4);
                    if (dist > 0)
                        recive_msg = hz + "," + hv + "," + dist.ToString();
                    else
                        recive_msg = "0";
                }
                else
                {
                    recive_msg = "0";
                }
            }
            else
            {
                recive_msg = "0";
            }
            end_send();
        }
        #endregion
        #region Open_Close_ATR
        /// <summary>
        /// 打开或关闭ATR
        /// </summary>
        /// <param name="on_off">打开为1，关闭为0</param>
        /// <returns>成功返回1，失败返回0</returns>
        public string Open_Close_ATR(string on_off)
        {
            Open_ATR_del del = new Open_ATR_del(Open_ATR_thr);
            del.Invoke(on_off);
            string val = recive_msg;
            return val;
        }
        delegate void Open_ATR_del(string on_off);
        void Open_ATR_thr(string val)
        {
            if (listen == true)
            {
                return;
            }
            send("%R1Q,18005:" + val.ToString() + "\r\n");
            wait.WaitOne(3000, false);
            if (recive_msg != null && recive_msg.Length > 0)
            {
                recive_msg = recive_msg.Replace("'", string.Empty);
                string[] receive = recive_msg.Split(Convert.ToChar(","));
                if (receive[2] == "0:0")
                {
                    recive_msg = "1";

                }
                else
                {
                    recive_msg = "0";
                }


            }
            else
            {
                recive_msg = "0";
            }
            end_send();
        }
        #endregion
        #region Make_Postion
        /// <summary>
        /// 转动到指定的角度
        /// </summary>
        /// <param name="hz_hv">水平角和垂直角(弧度)用逗号隔开</param>
        /// <returns>成功返回1，失败返回0</returns>
        public string Make_Postion(string hz_hv)
        {
            make_postion_del del = new make_postion_del(make_postion_thr);
            del.Invoke(hz_hv);
            string val = recive_msg;
            return val;

        }
        delegate void make_postion_del(string hz_hv);
        void make_postion_thr(object hzhv)
        {
            if (listen == true)
            {
                return;
            }
            string[] para = hzhv.ToString().Split(Convert.ToChar(","));
            string hz = para[0];
            string hv = para[1];
            send("%R1Q,9027:" + hz + "," + hv + ",0,0,0" + "\r\n");
            wait.WaitOne(15000, false);
            if (recive_msg != null && recive_msg.Length > 0)
            {
                recive_msg = recive_msg.Replace("'", string.Empty);
                string[] receive = recive_msg.Split(Convert.ToChar(","));
                if (receive[2] == "0:0")
                {
                    recive_msg = "1";
                }
                else
                {
                    recive_msg = "0";
                }


            }
            else
            {
                recive_msg = "0";
            }
            end_send();
        }
        /// <summary>
        /// 转动到指定的角度
        /// </summary>
        /// <param name="hz_hv">水平角和垂直角(弧度)用逗号隔开</param>
        /// <returns>成功返回1，失败返回0</returns>
        public string Make_Postion1(string hz_hv)
        {
            make_postion_del del = new make_postion_del(make_postion_thr1);
            //IAsyncResult result = del.BeginInvoke(hz_hv, null, null);
            del.Invoke(hz_hv);
            string val = recive_msg;
            return val;

        }
        void make_postion_thr1(object hzhv)
        {
            if (listen == true)
            {
                return;
            }
            string[] para = hzhv.ToString().Split(Convert.ToChar(","));
            string hz = para[0];
            string hv = para[1];
            send("%R1Q,9027:" + hz + "," + hv + ",0,1,0" + "\r\n");
            wait.WaitOne(15000, false);
            if (recive_msg != null && recive_msg.Length > 0)
            {
                recive_msg = recive_msg.Replace("'", string.Empty);
                string[] receive = recive_msg.Split(Convert.ToChar(","));
                if (receive[2] == "0:0")
                {
                    recive_msg = "1";
                }
                else
                {
                    recive_msg = "0";
                }


            }
            else
            {
                recive_msg = "0";
            }
            end_send();
        }

        #endregion
        #region Set_measure_Mode
        /// <summary>
        /// 设置测量模式为RL或IR
        /// </summary>
        /// <param name="RL_IR">RL为无棱镜,IO为有棱镜</param>
        /// <returns>成功返回1，失败返回0</returns>
        public string Set_Measure_Mode(string RL_IR)
        {
            Set_Measure_Mode_Del del = new Set_Measure_Mode_Del(Set_Measure_Mode_Thr);
            IAsyncResult result = del.BeginInvoke(RL_IR, null, null);
            while (!result.IsCompleted)
            { }
            del.EndInvoke(result);
            string val = recive_msg;
            return val;
        }
        delegate void Set_Measure_Mode_Del(string RL_IR);
        void Set_Measure_Mode_Thr(object val)
        {
            if (listen == true)
            {
                return;
            }
            string index = "";
            if (val.ToString() == "RL")
            {
                index = "3";
            }
            else if (val.ToString() == "IR")
            {
                index = "0";
            }

            send("%R1Q,17019:" + index + "\r\n");
            wait.WaitOne(5000, false);
            if (recive_msg != null && recive_msg.Length > 0)
            {
                recive_msg = recive_msg.Replace("'", string.Empty);
                string[] receive = recive_msg.Split(Convert.ToChar(","));
                if (receive[2] == "0:0")
                {
                    recive_msg = "1";
                }
                else
                {
                    recive_msg = "0";
                }


            }
            else
            {
                recive_msg = "0";
            }
            end_send();
        }
        #endregion
        #region Set_Prism
        /// <summary>
        /// 设置棱镜参数类型
        /// </summary>
        /// <param name="prim_type">0圆棱镜,1小棱镜,2反射片,3,360°棱镜</param>
        /// <returns>成功返回1，失败返回0</returns>
        public string Set_Prism(string prim_type)
        {
            set_prism_Del del = new set_prism_Del(set_prism_thr);
            IAsyncResult result = del.BeginInvoke(prim_type, null, null);
            while (!result.IsCompleted)
            { }
            del.EndInvoke(result);
            string val = recive_msg;
            return val;
        }
        //        圆棱镜
        //小棱镜
        //反射片
        //360°棱镜
        delegate void set_prism_Del(string prim_type);
        void set_prism_thr(object val)
        {
            if (listen == true)
            {
                return;
            }
            string index = val.ToString();
            send("%R1Q,17008:" + index.ToString() + "\r\n");
            wait.WaitOne(5000, false);
            if (recive_msg != null && recive_msg.Length > 0)
            {
                recive_msg = recive_msg.Replace("'", string.Empty);
                string[] receive = recive_msg.Split(Convert.ToChar(","));
                if (receive[2] == "0:0")
                {
                    recive_msg = "1";
                }
                else
                {
                    recive_msg = "0";
                }
            }
            else
            {
                recive_msg = "0";
            }
            end_send();

        }

        #endregion
        #region Set_Orientaion
        /// <summary>
        /// 定向,水平角设为0°
        /// </summary>
        /// <returns>成功返回1，失败返回0</returns>
        public string Set_Orientaion(string rag)
        {
            Set_Orientaion_del del = new Set_Orientaion_del(Set_Orientaion_thr);
            del.Invoke(rag);
            string val = recive_msg;
            return val;
        }
        delegate void Set_Orientaion_del(string rag);
        void Set_Orientaion_thr(object val)
        {
            string rag = val.ToString();
            if (listen == true)
            {
                return;
            }
            send("%R1Q,2113:" + rag + "\r\n");
            wait.WaitOne(5000, false);
            if (recive_msg != null && recive_msg.Length > 0)
            {
                recive_msg = recive_msg.Replace("'", string.Empty);
                string[] receive = recive_msg.Split(Convert.ToChar(","));
                if (receive[2] == "0:0")
                {
                    recive_msg = "1";
                }
                else
                {
                    recive_msg = "0";
                }
            }
            else
            {
                recive_msg = "0";
            }
            end_send();
        }
        #endregion
        #region Change_Face
        /// <summary>
        /// 倒镜观测
        /// </summary>
        /// <returns>成功返回1，失败返回0</returns>
        public string Change_Face()
        {
            Change_Face_del del = new Change_Face_del(Change_Face_Thr);
            IAsyncResult result = del.BeginInvoke(null, null);
            while (!result.IsCompleted)
            { }
            del.EndInvoke(result);
            string val = recive_msg;
            return val;
        }
        delegate void Change_Face_del();
        void Change_Face_Thr()
        {
            if (listen == true)
            {
                return;
            }
            send("%R1Q,9028:1,0,0" + "\r\n");
            wait.WaitOne(15000, false);
            if (recive_msg != null && recive_msg.Length > 0)
            {
                recive_msg = recive_msg.Replace("'", string.Empty);
                string[] receive = recive_msg.Split(Convert.ToChar(","));
                if (receive[2] == "0:0")
                {
                    recive_msg = "1";
                }
                else
                {
                    recive_msg = "0";
                }
            }
            else
            {
                recive_msg = "0";
            }
            end_send();
        }
        #endregion
        #region SetStation
        /// <summary>
        /// 设站
        /// </summary>
        /// <param name="station_coor">E,N,H,Hi</param>
        /// <returns>1或0</returns>
        public string Set_Station(string station_coor)
        {
            Set_station_del del = new Set_station_del(Set_Station_thr);
            IAsyncResult result = del.BeginInvoke(station_coor, null, null);
            while (!result.IsCompleted)
            { }
            del.EndInvoke(result);
            string val = recive_msg;
            return val;
        }
        delegate void Set_station_del(string station_coor);
        void Set_Station_thr(string val)
        {

            if (listen == true)
            {
                return;
            }
            string[] coor = val.ToString().Split(Convert.ToChar(","));
            string index = val.ToString();
            send("%R1Q,2010:" + coor[0] + "," + coor[1] + "," + coor[2] + "," + coor[3] + "\r\n");
            wait.WaitOne(15000, false);
            if (recive_msg != null && recive_msg.Length > 0)
            {
                recive_msg = recive_msg.Replace("'", string.Empty);
                string[] receive = recive_msg.Split(Convert.ToChar(","));
                if (receive[2] == "0:0")
                {
                    recive_msg = "1";
                }
                else
                {
                    recive_msg = "0";
                }
            }
            else
            {
                recive_msg = "0";
            }
            end_send();
        }
        #endregion



        #region 打开或关闭激光
        /// <summary>
        /// 打开或关闭指示激光
        /// </summary>
        /// <param name="on_off">1为打开,0为关闭</param>
        public string Set_Laser(string on_off)
        {
            make_postion_del del = new make_postion_del(Set_Laser_Thr);
            IAsyncResult result = del.BeginInvoke(on_off, null, null);
            while (!result.IsCompleted)
            { }
            del.EndInvoke(result);
            string val = recive_msg;
            return val;
        }


        void Set_Laser_Thr(string onoff)
        {
            if (listen == true)
            {
                return;
            }

            send("%R1Q,1004:" + onoff + "\r\n");
            wait.WaitOne(15000, false);
            if (recive_msg != null && recive_msg.Length > 0)
            {
                recive_msg = recive_msg.Replace("'", string.Empty);
                string[] receive = recive_msg.Split(Convert.ToChar(","));
                if (receive[2] == "0:0")
                {
                    recive_msg = "1";
                }
                else
                {
                    recive_msg = "0";
                }


            }
            else
            {
                recive_msg = "0";
            }
            end_send();
        }

        #endregion
        #region GetStation
        /// <summary>
        /// 获取测站坐标
        /// </summary>
        /// <returns>E,N,H,Hi</returns>
        public string Get_Station()
        {

            Get_Station_del del = new Get_Station_del(Get_Station_Thr);
            //IAsyncResult result = del.BeginInvoke(null, null);
            del.Invoke();
            string val = recive_msg;
            return val;
        }
        delegate void Get_Station_del();
        void Get_Station_Thr()
        {
            if (listen == true)
            {
                return;
            }
            send("%R1Q,2009:\r\n");
            wait.WaitOne(5000, false);
            if (recive_msg != null && recive_msg.Length > 0)
            {
                recive_msg = recive_msg.Replace("'", string.Empty);
                string[] receive = recive_msg.Split(Convert.ToChar(","));
                if (receive[2] == "0:0")
                {
                    recive_msg = receive[3] + "," + receive[4] + "," + receive[5] + "," + receive[6];
                }
                else
                {
                    recive_msg = "0";
                }
            }
            else
            {
                recive_msg = "0";
            }
            end_send();
        }
        #endregion
        #region GetFace
        /// <summary>
        /// 获取正倒镜
        /// </summary>
        /// <returns>1为正,-1为负,0为失败</returns>
        public string Get_Face()
        {

            Get_Station_del del = new Get_Station_del(Get_Face_thr);
            IAsyncResult result = del.BeginInvoke(null, null);
            while (!result.IsCompleted)
            { }
            del.EndInvoke(result);
            string val = recive_msg;
            return val;
        }
        void Get_Face_thr()
        {
            if (listen == true)
            {
                return;
            }
            send("%R1Q,2026:\r\n");
            wait.WaitOne(5000, false);
            if (recive_msg != null && recive_msg.Length > 0)
            {
                recive_msg = recive_msg.Replace("'", string.Empty);
                string[] receive = recive_msg.Split(Convert.ToChar(","));
                if (receive[2] == "0:0")
                {
                    if (receive[3] == "0")
                    {
                        recive_msg = "1";
                    }
                    else
                    {
                        recive_msg = "-1";
                    }
                }
                else
                {
                    recive_msg = "0";
                }


            }
            else
            {
                recive_msg = "0";
            }
            end_send();
        }

        #endregion
        #region Get_Coordinate
        /// <summary>
        /// 进行一次测量,获取坐标,返回E,N,H,Hz,Hv,Slope
        /// </summary>
        /// <returns>E,N,H</returns>
        public string Get_Coordinate()
        {
            Get_Coordinate_del del = new Get_Coordinate_del(Get_Coordinate_thr);
            IAsyncResult result = del.BeginInvoke(null, null);
            while (!result.IsCompleted)
            { }
            del.EndInvoke(result);
            string val = recive_msg;
            return val;
        }
        delegate void Get_Coordinate_del();
        void Get_Coordinate_thr()
        {
            if (listen == true)
            {
                return;
            }
            send("%R1Q,2008:1,1\r\n");
            wait.WaitOne(5000, false);
            if (recive_msg != null && recive_msg.Length > 0)
            {
                recive_msg = recive_msg.Replace("'", string.Empty);
                string[] receive = recive_msg.Split(Convert.ToChar(","));
                if (receive[2] == "0:0")
                {
                    wait.Reset();
                    send("%R1Q,2082:5000,1\r\n");
                    wait.WaitOne(5000, false);
                    if (recive_msg != null && recive_msg.Length > 0)
                    {
                        recive_msg = recive_msg.Replace("'", string.Empty);
                        receive = recive_msg.Split(Convert.ToChar(","));
                        if (receive[2] == "0:0")
                        {
                            wait.Reset();
                            string temp = receive[3] + "," + receive[4] + "," + receive[5];
                            recive_msg = temp;
                            send("%R1Q,2108:5000,1\r\n");
                            wait.WaitOne(5000, false);
                            if (recive_msg != null && recive_msg.Length > 0)
                            {
                                recive_msg = recive_msg.Replace("'", string.Empty);
                                receive = recive_msg.Split(Convert.ToChar(","));
                                if (receive[2] == "0:0")
                                {
                                    recive_msg = temp + "," + receive[3] + "," + receive[4] + "," + receive[5];
                                }
                                else
                                {
                                    recive_msg = "0";
                                }


                            }
                            else
                            {
                                recive_msg = "0";
                            }
                        }
                        else
                        {
                            recive_msg = "0";
                        }


                    }
                    else
                    {
                        recive_msg = "0";
                    }
                    end_send();

                }
                else
                {
                    recive_msg = "0";
                }


            }
            else
            {
                recive_msg = "0";
            }
            end_send();
        }

        #endregion
        #region Shut_Off
        /// <summary>
        /// 关机
        /// </summary>
        /// <returns></returns>
        public string Shut_Off()
        {
            Shut_off_del del = new Shut_off_del(shut_off);
            IAsyncResult result = del.BeginInvoke(null, null);
            while (!result.IsCompleted)
            { }
            del.EndInvoke(result);
            string val = recive_msg;
            return val;
        }
        delegate void Shut_off_del();
        void shut_off()
        {


            if (listen == true)
            {
                return;
            }
            send(" %R1Q,112:0\r\n");
            wait.WaitOne(5000, false);
            if (recive_msg != null && recive_msg.Length > 0)
            {
                recive_msg = recive_msg.Replace("'", string.Empty);
                string[] receive = recive_msg.Split(Convert.ToChar(","));
                if (receive[2] == "0:0")
                {

                    recive_msg = "1";

                }
                else
                {
                    recive_msg = "0";
                }


            }
            else
            {
                recive_msg = "0";
            }
            end_send();
        }

        #endregion
        #region Get_Reflector_Height
        /// <summary>
        /// 读取棱镜高
        /// </summary>
        /// <returns>米,棱镜高</returns>
        public string Get_Reflector_Height()
        {
            Get_Reflector_Height_del del = new Get_Reflector_Height_del(Get_Reflector_Height_thr);
            IAsyncResult result = del.BeginInvoke(null, null);
            while (!result.IsCompleted)
            { }
            del.EndInvoke(result);
            string val = recive_msg;
            return val;
        }
        delegate void Get_Reflector_Height_del();
        void Get_Reflector_Height_thr()
        {
            if (listen == true)
            {
                return;
            }
            send("%R1Q,2011:\r\n");
            wait.WaitOne(5000, false);
            if (recive_msg != null && recive_msg.Length > 0)
            {
                recive_msg = recive_msg.Replace("'", string.Empty);
                string[] receive = recive_msg.Split(Convert.ToChar(","));
                if (receive[2] == "0:0")
                {

                    recive_msg = receive[3];

                }
                else
                {
                    recive_msg = "0";
                }


            }
            else
            {
                recive_msg = "0";
            }
            end_send();
        }


        #endregion
        #region Set_Reflector_Height
        /// <summary>
        /// 设置棱镜高
        /// </summary>
        /// <param name="height">棱镜高,米</param>
        /// <returns>1或0</returns>
        public string Set_Reflector_Height(string height)
        {
            Set_Reflector_Height_del del = new Set_Reflector_Height_del(Set_Reflector_Height_thr);
            IAsyncResult result = del.BeginInvoke(height, null, null);
            while (!result.IsCompleted)
            { }
            del.EndInvoke(result);
            string val = recive_msg;
            return val;
        }
        delegate void Set_Reflector_Height_del(string height);
        void Set_Reflector_Height_thr(string height)
        {
            if (listen == true)
            {
                return;
            }
            send("%R1Q,2012:" + height + "\r\n");
            wait.WaitOne(5000, false);
            if (recive_msg != null && recive_msg.Length > 0)
            {
                recive_msg = recive_msg.Replace("'", string.Empty);
                string[] receive = recive_msg.Split(Convert.ToChar(","));
                if (receive[2] == "0:0")
                {

                    recive_msg = "1";

                }
                else
                {
                    recive_msg = "0";
                }


            }
            else
            {
                recive_msg = "0";
            }
            end_send();
        }
        #endregion
        #region Set_Presion
        /// <summary>
        /// 设置小数精度
        /// </summary>
        /// <param name="dn">小数位数</param>
        /// <returns></returns>
        public string Set_presion(string dn)
        {
            make_postion_del del = new make_postion_del(Set_Laser_Thr);
            IAsyncResult result = del.BeginInvoke(dn, null, null);
            while (!result.IsCompleted)
            { }
            del.EndInvoke(result);
            string val = recive_msg;
            return val;
        }
        void Set_presion_Thr(string dn)
        {
            if (listen == true)
            {
                return;
            }

            send("%R1Q,107:" + dn + "\r\n");
            wait.WaitOne(15000, false);
            if (recive_msg != null && recive_msg.Length > 0)
            {
                recive_msg = recive_msg.Replace("'", string.Empty);
                string[] receive = recive_msg.Split(Convert.ToChar(","));
                if (receive[2] == "0:0")
                {
                    recive_msg = "1";
                }
                else
                {
                    recive_msg = "0";
                }


            }
            else
            {
                recive_msg = "0";
            }
            end_send();
        }
        #endregion


        #region related functions

        private void ClosePort(SerialPort port)
        {
            try
            {
                if (port != null)
                {
                    port.Close();
                    port.DataReceived -= new SerialDataReceivedEventHandler(port_DataReceived);
                    port = null;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 打开port
        /// </summary>
        /// <param name="p_strPortName">COM1，COM2之类的</param>
        /// <param name="p_uBaudRate">波特率</param>
        /// <param name="p_uDataBits">位数</param>
        /// <param name="p_uReadTimeout">读延迟</param>
        /// <param name="p_uWriteTimeout">写延迟</param>
        /// <returns></returns>
        public SerialPort OpenPort(string p_strPortName, int p_uBaudRate, int p_uDataBits, int p_uReadTimeout, int p_uWriteTimeout)
        {
            wait = new AutoResetEvent(false);


            try
            {
                port.PortName = p_strPortName;                 //COM1
                port.BaudRate = p_uBaudRate;                   //9600
                port.DataBits = p_uDataBits;                   //8
                port.StopBits = StopBits.One;                  //1
                port.Parity = Parity.None;                     //None
                port.ReadTimeout = p_uReadTimeout;             //300
                port.WriteTimeout = p_uWriteTimeout;           //300
                // port.Encoding = Encoding.ASCII;
                //port.Encoding = Encoding.Unicode;
                port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
                port.ReadBufferSize = 8192;
                port.DtrEnable = true;

                port.Open();

                // port.RtsEnable = true;
                return port;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
                return null;

            }

        }
        //收到port的数据
        void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            datareceive();
        }
        void datareceive()
        {
            try
            {

                int bytes_no = port.BytesToRead;
                if (bytes_no == 0)
                {
                    return;
                }
                char[] chars = new char[bytes_no];
                int bread = port.Read(chars, 0, bytes_no);
                recive_msg = recive_msg + new string(chars, 0, bytes_no);


            }
            catch (Exception ex)
            {
                throw ex;
            }
            string substr = "";
            if (recive_msg.Length > 2)
                substr = recive_msg.Substring(recive_msg.Length - 2, 2);



            if (substr != "\r\n")
            {
                datareceive();

            }
            else
            {
                if (listen == true)
                {
                    recive_msg = recive_msg.Substring(0, recive_msg.Length - 2);
                    wait.Set();
                }
                else
                {
                    recive_msg = "";
                    wait.Reset();
                }

                return;
            }
        }
        private void send(string cmd)
        {

            port.BaseStream.Flush();
            string sendmsg = cmd;// 
            port.Write(sendmsg.ToCharArray(), 0, sendmsg.Length);
            recive_msg = "";
            port.BaseStream.Flush();
            listen = true;
        }
        private void end_send()
        {
            listen = false;
            wait.Reset();
        }
        //弧度到度分秒
        private string cal_DMS(string p)
        {
            //double DMS = Convert.ToDouble(p);
            //DMS = DMS / Math.PI * 180;



            //double D = Math.Floor(DMS);
            //double M = Math.Floor((DMS - D) * 60);
            //double S = Math.Floor(((DMS - D) * 60 - M) * 60);


            //return D.ToString() + "." + M.ToString() + "." + S.ToString();
            return p;
        }
        #endregion

    }
}
